# coding: utf-8
import json

from flask import Blueprint, request

import extentions.db
from bluesky.converter import resource_converter
from bluesky.dto.Response import Response
from bluesky.repository import resource_repository
from bluesky.util import biz

idx_blue = Blueprint('index', __name__, url_prefix='/')


@idx_blue.route("/")
def index():
    return {'status': 1111}


@idx_blue.route('/test_render', methods=['POST', 'GET'])
def test():
    # tpl = "select * from resource_api where is_delete=0 {% if body.name %} and name='{{body.name}}' {% endif %}" \
    #       "order by created_time desc limit 1"
    # tpl = "{{body.name == 'api1'}}"
    tpl = {"a": "{{body.name}}"}
    tpl_str = json.dumps(tpl)
    render_tpl = biz.render(tpl_str, biz.request_context(''))
    result = json.loads(render_tpl)
    return result


@idx_blue.route('/test_list/<rtype>', methods=['POST', 'GET'])
def test_list(rtype):
    data = request.get_json()
    l = resource_repository.select_list(extentions.db.new_session(), rtype, data)
    # total = resource_repository.select_count(rtype, data)
    return Response.success(resource_converter.to_dtos(l))

